package com.example.class_manage.Controller;


import com.example.class_manage.Entity.User;
import com.example.class_manage.Mapper.Usermapper;
import com.example.class_manage.Util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author Songxiao
 * @version 1.0
 * @date 2021/11/8 19:54
 */
@Slf4j
@Api(tags = "判断用户信息身份接口")
@RestController
@RequestMapping("/")
public class Apicontroller {
    @Autowired
    Usermapper usermapper;

    @ApiOperation("判断是不是老师")
    @PostMapping("/isteacher")
    public boolean isteacher(int id) {
        User user = usermapper.queryUser(id);
        if (user.getFlag() == 1)
            return true;
        else
            return false;
    }

    @ApiOperation("判断是不是助教")
    @PostMapping("/isassistant")
    public boolean isassistant(int id) {
        User user = usermapper.queryUser(id);
        if (user.getFlag() == 3)
            return true;
        else
            return false;
    }

    @ApiOperation("判断是不是学生")
    @PostMapping("/isstudent")
    public boolean isstudent(int id) {
        User user = usermapper.queryUser(id);
        if (user.getFlag() == 2)
            return true;
        else
            return false;
    }

    @PostMapping("/action")
    public JsonData action(@RequestParam("id") int id, HttpSession session) {
        System.out.println(id);
        User user = usermapper.queryUser(id);
        session.setAttribute("user", user);
        System.out.println(action(session));
        return JsonData.buildSuccess(user);
    }

    public User action(HttpSession session) {
        User user = (User) session.getAttribute("user");
        return user;
    }

}
